package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.view.CustomChooseView;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;

/**
 * Created by Administrator on 2016/3/9 0009.
 */
public class TradeStateActivity extends ABBaseActivity implements View.OnClickListener {

    private String[] states = {"已成功", "已失败", "初始化", "已冲正", "已撤销", "已退款", "超额", "已结算", "已完成", "关闭"};
    private String[] statusIds = {"SUCCESS", "FAILED", "INIT", "REVERSED", "REVOKED", "REFUND", "OVERLIMIT", "SETTLE", "COMPLETE", "CLOSED"};

    private TitleBar titleBar;
    private LinearLayout layout_content;
    Intent intent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_trade_state;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
        titleBar.setShowRight(View.GONE);
        //add by zhuangzeqin 2017年4月24日13:35:45 隐藏右边确定按钮
        titleBar.setRightTextView("确定");
        titleBar.setRightTextColor(R.color.unify_grounding_white);

        layout_content = getViewById(R.id.layout_content);
        for(int i = 0; i < states.length; i ++){
            CustomChooseView chooseView = new CustomChooseView(mContext);
            chooseView.setTitle(states[i]);
            chooseView.setTag(i);
            chooseView.setOnClickListener(this);
            layout_content.addView(chooseView);
        }
    }

    @Override
    protected void eventOnClick() {
        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
            @Override
            public void onRightClick(View view) {
                getSelectStatusResult();
            }
        });
    }

    /**
     * 获得选中的结果
     */
    private void getSelectStatusResult() {
        intent = new Intent();
        for (int i = 0; i < layout_content.getChildCount(); i++) {
            CustomChooseView cv = (CustomChooseView) layout_content.getChildAt(i);
            if (cv.getIsSelected()) {
                intent.putExtra("trade_state", cv.getLeftTitle());
                intent.putExtra("trade_status_id", statusIds[i]);
            }
        }
        setResult(RESULT_OK, intent);
        TradeStateActivity.this.finish();
    }

    /**
     * 单选
     * @param index
     */
    private void singleSelect(int index){
        for (int i = 0; i < layout_content.getChildCount(); i++){
            CustomChooseView childView = (CustomChooseView) layout_content.getChildAt(i);
            if(i == index){
                childView.setIsSelected(true);
            }else{
                childView.setIsSelected(false);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getTag() != null){
            int index = (int) v.getTag();
            singleSelect(index);
        }
        getSelectStatusResult();
    }
}
